package wangm.fastdfs;

import org.junit.Test;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


/**
 * fast dfs
 *
 * @author zk
 * @date 2018-02-02 10:58
 */
public class FastDfsTest {
    /**
     * fast dfs url
     */
    public static final String FASTDFS_URL = "http://192.168.18.91:81";
    /**
     * user id
     */
    public static final String FASTDFS_USERID = "460f24ada867069a";
    /**
     * user secret
     */
    public static final String FASTDFS_USERSECRET = "77ea042a9b76fe7e";

    public static String hamcsha1(String data, String key){
        try {
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            return byteArrayToHex(mac.doFinal(data.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String byteArrayToHex(byte[] byteArray) {
        // 首先初始化一个字符数组，用来存放每个16进制字符
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        // new一个字符数组，这个就是用来组成结果字符串的（解释一下：
        // 一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方））
        char[] resultCharArray = new char[byteArray.length * 2];
        // 遍历字节数组，通过位运算（位运算效率高），转换成字符放到字符数组中去
        int index = 0;
        for (byte b : byteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        // 字符数组组合成字符串返回
        return (new String(resultCharArray)).toLowerCase();
    }
    @Test
    public void uploadFileTest(){
        System.out.println(hamcsha1(FASTDFS_USERID + "1518558768000",FASTDFS_USERSECRET));
    }
}
